<?php
require '../../../zb_system/function/c_system_base.php';

require '../../../zb_system/function/c_system_admin.php';

$zbp->Load();

$action='root';
if (!$zbp->CheckRights($action)) {$zbp->ShowError(6);die();}

if (!$zbp->CheckPlugin('BomCheck')) {$zbp->ShowError(48);die();}

$blogtitle='BOM文件头检查';

require $blogpath . 'zb_system/admin/admin_header.php';
require $blogpath . 'zb_system/admin/admin_top.php';

function BomCheck_CheckNow($f) {
	$zbp=ZBlogPHP::GetInstance();
	$n='';
	if(BomCheck_Check($f)){
		$n='&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="RemoveBOM(\''.base64_encode($f).'\',this);" title="Remove BOM"><img src="'.$zbp->host.'zb_users/plugin/BomCheck/error.png" /></a>';
		echo '<tr><th>&nbsp;&nbsp;&nbsp;&nbsp;' . $f . '</th><th id="_s">' . $n . '</th></tr>';
	}
}

function BomCheck_Check($f)
{
	$contents = file_get_contents($f);
	$char[1] = substr($contents, 0, 1); // 1
	$char[2] = substr($contents, 1, 1); // 2
	$char[3] = substr($contents, 2, 1); // 3
	// EF BB BF
	if ( ord($char[1]) == 239 && ord($char[2]) == 187 && ord($char[3]) == 191 ) 
	{
		return true;
	}
	return false;
}

function BomCheck_GetDirsInDir($dir) {
	$dirs = array();

	if (function_exists('scandir')) {
		foreach (scandir($dir) as $d) {
			if (is_dir($dir . $d)) {
				if (($d <> '.') && ($d <> '..')) {
					$dirs[] = $dir . $d . '/';
				}
			}
		}
	} else {
		if ($handle = opendir($dir)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != "..") {
					if (is_dir($dir . $file)) {
						$dirs[] = $dir . $file . '/';
					}
				}
			}
			closedir($handle);
		}
	}

	return $dirs;
}



function BomCheck_GetFilesInDir($dir, $type){

$d=BomCheck_GetDirsInDir($dir);

$f=GetFilesInDir($dir, $type);

foreach ($d as $sub){
	$f=$f + BomCheck_GetFilesInDir($sub, $type);
}

return $f;

}

?>
<div id="divMain">
  <div class="divHeader2"><?php echo $blogtitle;?></div>
  <div class="SubMenu"></div>
  <div id="divMain2">

<form class="search" id="search" method="post" action="main.php">
扩展名：<input name="search" style="width:500px;" type="text" value="php|inc|html|css|js" />(以|号做为分隔) &nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" class="button" value="提交"/>
</form>
<?php

$ext=GetVars('search');
if($ext!=''){

$a=BomCheck_GetFilesInDir($zbp->path,$ext);

echo '<table border="1" width="100%" cellspacing="0" cellpadding="0" class="tableBorder tableBorder-thleft">
                <tbody><tr>
                  <th width="88%">文件名</th>
                  <th id="_s">状态</th>
                </tr>';
				
foreach ($a as $f){
	BomCheck_CheckNow($f);
}

echo '</tbody></table>';

}
?>

	<script type="text/javascript">ActiveLeftMenu("aBomCheck");</script>
	<script type="text/javascript">AddHeaderIcon("<?php echo $bloghost . 'zb_users/plugin/BomCheck/logo.png';?>");</script>
	<script type="text/javascript">
function RemoveBOM(f,a){
	$.get(bloghost+"zb_users/plugin/BomCheck/remove.php?f="+f, function(data){
		if(data)$(a).html(data);
	});
}
	</script>
  </div>
</div>


<?php
require $blogpath . 'zb_system/admin/admin_footer.php';

RunTime();
?>